package com.xgr.wonderful.sns;

import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.R;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

public class SharePopWindow extends PopupWindow implements OnClickListener {
		
	private final UMSocialService mController = UMServiceFactory
            .getUMSocialService(Constant.DESCRIPTOR);
	
	private Context context;
	
	/**
	 * 将各种平台的分享内容一起设置好.
	 * @param context
	 * @param content  主要文字内容
	 * @param title  标题
	 * @param targetUrl  跳转url，点击分享后会跳转，可以设置成status页面、weitu主页 
	 * @param imageUrl 分享显示的主图片，比如status里面的图片
	 * @param bitmap 分享显示的主图片，比如status里面的图片,
	 * 说明有图片要分享,但是我们只能要一个,两个都有时取bitmap 
	 */
	public SharePopWindow(Context context, String content , String title , String targetUrl, String imageUrl, Bitmap bitmap ){
		this.context= context;
		initView();
		configPlatforms();
		/** 添加内容 **/
		setShareContent(content, title, targetUrl, imageUrl ,bitmap );
	}
	
	private void initView() {
		View rootView = LayoutInflater.from(context).
				inflate(R.layout.popwindow_share, null);
		rootView.findViewById(R.id.wechat).setOnClickListener(this);
		rootView.findViewById(R.id.wechat_circle).setOnClickListener(this);
		rootView.findViewById(R.id.qq).setOnClickListener(this);
		rootView.findViewById(R.id.qzone).setOnClickListener(this);
		rootView.findViewById(R.id.weibo).setOnClickListener(this);
		rootView.findViewById(R.id.share_dimiss_tv).setOnClickListener(this );
		setContentView(rootView);
		
		setWidth(android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		setHeight(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		setFocusable(true);
		ColorDrawable cd = new ColorDrawable(0x000000);//透明
		setBackgroundDrawable(cd);
		WindowManager.LayoutParams lp= ((Activity)context).getWindow().getAttributes();
		lp.alpha= 0.5f;
		((Activity)context).getWindow().setAttributes(lp);
		setTouchable(true);
		setOutsideTouchable(true) ;
		setAnimationStyle(R.style.umeng_socialize_dialog_animations) ;
		update();
		setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				dismissPop();
			}
		}) ;
		
	}
	
	/**
	 * 设置平台  weixin ,qq ,weibo
	 */
	private void configPlatforms(){
		/** 设置微信平台**/
		addWXPlatform();  
		
		addQQPlatform();
		
		//添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
	}
	
	private void addWXPlatform (){
		String appId = Constant.WeiXin_APP_ID;
		String appSecret = Constant.WeiXin_APP_Secret;
		 // 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(context, appId, appSecret);
		wxHandler .addToSocialSDK();
		
		// 支持微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(context, appId, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
	}
	
	
	private void addQQPlatform(){
		String appId = Constant.QQ_APP_ID;
		String appKey = Constant.QQ_APP_KEY;
		
		// 添加QQ支持, 并且设置QQ分享内容的target url
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler((Activity) context, appId, appKey);
		qqSsoHandler.addToSocialSDK();
		
		//qqzone
		QZoneSsoHandler  qZoneSsoHandler = new QZoneSsoHandler((Activity) context, 
				appId, appKey);
		qZoneSsoHandler.addToSocialSDK();
		
	}
	
	
	
	/**
	 * 将各种平台的分享内容一起设置好
	 * @param content  主要文字内容
	 * @param title  标题
	 * @param targetUrl  跳转url，点击分享后会跳转，可以设置成status页面、weitu主页 
	 * @param imageUrl 分享显示的主图片，比如status里面的图片
	 * @param bitmap 分享显示的主图片，比如status里面的图片
	 */
	public void setShareContent (String content , String title , String targetUrl, String imageUrl, Bitmap bitmap ){
		UMImage shareImage = null ;
		if (bitmap!=null || imageUrl!= null){  
			//说明有图片要分享,但是我们只能要一个,两个都有时取bitmap 
			if (bitmap !=null && imageUrl!=null){
				shareImage = new UMImage(context, bitmap);
			}
			if (bitmap!= null){
				shareImage = new UMImage(context, bitmap);
			}
			if (imageUrl != null){
				shareImage = new UMImage(context, imageUrl);
			}
		}else {
			shareImage = new UMImage(context, R.drawable.logo);
		}
		//一定要设置有内容 
		if (content==null || content.length()==0){
			content = "来自微圈";
		}
		//设置微信好友分享内容
		WeiXinShareContent weixinContent = new WeiXinShareContent();
		//设置分享文字
		weixinContent.setShareContent(content);
		//设置title
		weixinContent.setTitle(title);
		//设置分享内容跳转URL
		weixinContent.setTargetUrl(targetUrl);
		//设置分享图片,图片只能32k
		weixinContent.setShareImage(shareImage);
		mController.setShareMedia(weixinContent);
		
		
		//设置微信朋友圈分享内容
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setShareContent(content);
		//设置朋友圈title,这是要区别对待
		circleMedia.setTitle(content); //not title 
		circleMedia.setShareImage(shareImage);
		circleMedia.setTargetUrl(targetUrl);
		mController.setShareMedia(circleMedia);
		
		 // 设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setShareContent(content);
        qzone.setTargetUrl(targetUrl);
        qzone.setTitle(title);
        qzone.setShareImage(shareImage);
        mController.setShareMedia(qzone);
        
        // 设置QQ分享内容
        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setShareContent(content);
        qqShareContent.setTitle(title);
        qqShareContent.setShareImage(shareImage);
        qqShareContent.setTargetUrl(targetUrl);
        mController.setShareMedia(qqShareContent);
        
        //sina weibo 分享
        SinaShareContent sinaContent = new SinaShareContent();
        sinaContent.setShareContent(content);
        sinaContent.setTargetUrl(targetUrl);
        sinaContent.setTitle(title);
        sinaContent.setShareImage(shareImage);
        mController.setShareMedia(sinaContent);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.weibo:
			performShare(SHARE_MEDIA.SINA);
			break;
		case R.id.wechat:
			performShare(SHARE_MEDIA.WEIXIN);
			break;
		case R.id.wechat_circle:
			performShare(SHARE_MEDIA.WEIXIN_CIRCLE);
			break;
		case R.id.qq:
			performShare(SHARE_MEDIA.QQ);
			break;
		case R.id.qzone:
			performShare(SHARE_MEDIA.QZONE);
			break;
		case R.id.share_dimiss_tv:
			dismiss();
			break;
		default:
			break;
		}
		//点击后就取消
		dismiss() ;
	}
	private void dismissPop(){
		WindowManager.LayoutParams lp= ((Activity)context).getWindow().getAttributes();
		lp.alpha= 1f;
		((Activity)context).getWindow().setAttributes(lp);
		dismiss();
	}
	
	private void performShare (SHARE_MEDIA platform){
		mController.postShare(context, platform, new SnsPostListener() {	
			@Override
			public void onStart() {
				 //Toast.makeText(context, "开始分享.", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
				String showText= platform.toString();
				if (eCode == StatusCode.ST_CODE_SUCCESSED){
					showText +="平台分享成功";
				}else {
					 String eMsg = "";
                     if (eCode == -101){
                         eMsg = "没有授权";
                     }
                     //Toast.makeText(context, "分享失败[" + eCode + "] " + eMsg,Toast.LENGTH_SHORT).show();
				}
				dismiss();//关闭对话框
			}
		});
	}
}
